package com.bbcare.comm;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.oms.plat.store.ibatis.IAccountApplicationDAO;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import sun.misc.BASE64Encoder;

public class ToolUtil {
  @Autowired
  private static IAccountApplicationDAO accountApplicationDAO;

  public static String resolveJsons(Map<String, Object> map)
    throws Exception
  {
    String json = (String)map.get("json");
     String projectId = (String)map.get("projectId");
     String appId = (String)map.get("appId");

    JSONObject jsonobject = JSONObject.parseObject(json);
   JSONArray jsonArray = (JSONArray)jsonobject.get("apps");
    if ((jsonArray != null) && (jsonArray.size() > 0)) {
      for (int i = 0; i < jsonArray.size(); i++) {
        JSONObject jsonobject1 = jsonArray.getJSONObject(i);
       if (appId.equals(jsonobject1.getString("id"))) {
           accountApplicationDAO.updateStatus(appId, 1);
          jsonobject1.put("status", Integer.valueOf(1));
         } else if (1 == jsonobject1.getInteger("status").intValue())
        {
         jsonobject1.put("status", Integer.valueOf(2));
        }
         if ((1 == jsonobject1.getInteger("status").intValue()) &&
        ("1".equals(jsonobject1.getString("systemFlag")))) {
         JSONArray jsonArray1 = (JSONArray)jsonobject1.get("auProjectRoleRel");
          if ((jsonArray1 != null) && (jsonArray1.size() > 0)) {
            for (int j = 0; j < jsonArray1.size(); j++) {
            if (projectId.equals(jsonArray1.getJSONObject(j).getString("projectId"))) {
                jsonArray1.getJSONObject(j).put("selectStatus", Integer.valueOf(1));
              }
              else if (1 == jsonArray1.getJSONObject(j).getInteger("selectStatus").intValue())
              {
               jsonArray1.getJSONObject(j).put("selectStatus", Integer.valueOf(2));
              }
            }
          }
        }
      }
    }

    return jsonobject.toJSONString();
  }





  public static String getIPAddress(HttpServletRequest request)
  {
    String ip = null;

    String ipAddresses = request.getHeader("X-Forwarded-For");

    if ((ipAddresses == null) || (ipAddresses.length() == 0) || ("unknown".equalsIgnoreCase(ipAddresses))) {
      ipAddresses = request.getHeader("Proxy-Client-IP");
    }

     if ((ipAddresses == null) || (ipAddresses.length() == 0) || ("unknown".equalsIgnoreCase(ipAddresses))) {
    ipAddresses = request.getHeader("WL-Proxy-Client-IP");
    }

     if ((ipAddresses == null) || (ipAddresses.length() == 0) || ("unknown".equalsIgnoreCase(ipAddresses))) {
      ipAddresses = request.getHeader("HTTP_CLIENT_IP");
    }

   if ((ipAddresses == null) || (ipAddresses.length() == 0) || ("unknown".equalsIgnoreCase(ipAddresses))) {
      ipAddresses = request.getHeader("X-Real-IP");
    }

  if ((ipAddresses != null) && (ipAddresses.length() != 0)) {
    ip = ipAddresses.split(",")[0];
    }

    if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ipAddresses))) {
      ip = request.getRemoteAddr();
    }
    return ip;
  }















  public static String changeToSHA(String strText, String strType)
    throws UnsupportedEncodingException
  {
     String strResult = "";


     if ((strText != null) && (strText.length() > 0))
    {
      try
      {
         MessageDigest messageDigest = MessageDigest.getInstance(strType);

         messageDigest.update(strText.getBytes("UTF-8"));

         byte[] byteBuffer = messageDigest.digest();
         BASE64Encoder encode = new BASE64Encoder();
         strResult = encode.encode(byteBuffer);
      } catch (NoSuchAlgorithmException e) {
         e.printStackTrace();
      }
    }

     strResult = strResult.toLowerCase();
     return strResult;
  }












  public static String MD5Encrept(String sourceStr, int flag)
    throws Exception
  {
     String result = "";
    try {
       MessageDigest md = MessageDigest.getInstance("MD5");
       md.update(sourceStr.getBytes());
       byte[] b = md.digest();

       StringBuffer buf = new StringBuffer("");
       for (int offset = 0; offset < b.length; offset++) {
         int i = b[offset];
         if (i < 0)
           i += 256;
         if (i < 16)
           buf.append("0");
         buf.append(Integer.toHexString(i));
      }
       if (flag == 32) {
         result = buf.toString();
      } else {
         result = buf.toString().substring(8, 24);
      }
    } catch (NoSuchAlgorithmException e) {
       throw new NoSuchAlgorithmException(e.toString());
    }

     return result;
  }

  public static String Encrept(String password) throws Exception {
     password = password + "@#_!2018bbcare16888";
     password = changeToSHA(password, "SHA-256");
     password = MD5Encrept(password, 32);
     return password;
  }
}


